//
//  MoreCommitTableViewCell.m
//  Findif
//
//  Created by Mac on 2019/3/20.
//  Copyright © 2019年 Cicel Chen. All rights reserved.
//

#import "MoreCommitTableViewCell.h"

@interface MoreCommitTableViewCell ()<UITextViewDelegate>

// 背景
@property (nonatomic, weak) UIView * backView;
// 头像
@property (nonatomic, weak) UIImageView * headImageView;
// 名称
@property (nonatomic, weak) UILabel * nameLab;
// 内容
@property (nonatomic, weak) UILabel * contentLabel;
// 时间
@property (nonatomic, weak) UILabel * timeLabel;
// 性别
@property (nonatomic, weak) UIImageView * genderImageView;
//下级评论背景
@property (nonatomic, weak)UIView * lineView;

@property (nonatomic, copy)NSString * ID;
@property (nonatomic, copy)NSString * user_name;

@property (nonatomic, weak)UITextView * textView;
@property (nonatomic, copy)NSString * reply_user_name;
@property (nonatomic, copy)NSString * reply_user_id;

@end

@implementation MoreCommitTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    if ([super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        [self setupUI];
    }
    
    return self;
}


- (void)setModel:(TopicCommitModel *)model{
    
    _model = model;
    
//    self.ID = [NSString stringWithFormat:@"%@",topicModel.id];
    self.user_name = model.user_name;
    
//    NSString * iconStr = [NSString stringWithFormat:@"%@%@",BASEURLSTR,model.head_img];
//    NSURL * url = [NSURL URLWithString:iconStr];
//    // 头像
//    [self.headImageView sd_setImageWithURL:url];
    if([model.head_img containsString:@"http"]) {
        
        NSURL * url = [NSURL URLWithString:model.head_img];
        [self.headImageView sd_setImageWithURL:url];
    } else {
        
        NSString * iconStr = [NSString stringWithFormat:@"%@%@",BASEURLSTR,model.head_img];
        NSURL * url = [NSURL URLWithString:iconStr];
        [self.headImageView sd_setImageWithURL:url];
    }
    // 昵称
    self.nameLab.text = model.user_name;
    int gender = [model.sex intValue];
    if (gender == 1) {
        
        self.genderImageView.image = [UIImage imageNamed:@"男性"];
    }else if (gender == 2){
        
        self.genderImageView.image = [UIImage imageNamed:@"女性"];
    }
    
    self.timeLabel.text = model.inputtime;
    CGFloat bottom = self.timeLabel.bottom + kPaddingValue;
    
    int is_second = [model.is_second intValue];
    
    if (is_second == 1) {

        self.contentLabel.text = model.content;
        
        CGSize attrStrSize = [self preferredSizeWithMaxWidth:275 * SCREENH6 withLabel:self.contentLabel];
        CGFloat labH = attrStrSize.height;
        
        self.contentLabel.frame = CGRectMake(self.nameLab.left, bottom, attrStrSize.width, labH);
        
        bottom = self.contentLabel.bottom + kPaddingValue;
    }else{
        
        self.reply_user_id = [NSString stringWithFormat:@"%@",model.reply_user_id];
        self.contentLabel.text = [NSString stringWithFormat:@"回复%@:%@",model.reply_user_name,model.content];
        CGSize subSize = [self preferredSizeWithMaxWidth:275 * SCREENH6 withLabel:self.contentLabel];

        self.reply_user_name = [NSString stringWithFormat:@"%@",model.reply_user_name];

        NSString *descText = [NSString stringWithFormat:@"回复%@:%@",self.reply_user_name,model.content];
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:descText];
        [attributedString addAttribute:NSLinkAttributeName
                                 value:@"Name:"
                                 range:[[attributedString string] rangeOfString:self.reply_user_name]];
        
        self.textView.attributedText = attributedString;
        
        self.textView.frame = CGRectMake(self.nameLab.left, bottom, 275 * SCREENH6, subSize.height + 8 * SCREENH6);
        
        bottom = self.textView.bottom + kPaddingValue;
    }

    
    self.lineView.frame = CGRectMake(0, (bottom - 1) * SCREENH6, 345 * SCREENH6, 1 * SCREENH6);
    self.backView.frame = CGRectMake(15 * SCREENH6, 0 * SCREENH6, 345 * SCREENH6, bottom);
    // 这样做就是起到缓存行高的作用，省去重复计算!!!
    
    _model.rowHeight = bottom;
}

- (void)setupUI{
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.backgroundColor = ColorWithRGB(0xF4F5F6);
    
    UIView * backView = [[UIView alloc] init];
    
    backView.backgroundColor = ColorWithRGB(0xF4F5F6);
    backView.userInteractionEnabled = YES;
    backView.layer.cornerRadius = 5 * SCREENH6;
    backView.layer.masksToBounds = YES;
    
    self.backView = backView;
    
    [self.contentView addSubview:self.backView];
    
    
    // 头像视图
    UIImageView * headImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10 * SCREENH6, kBlank, kFaceWidth, kFaceWidth)];
    headImageView.contentMode = UIViewContentModeScaleAspectFill;
    //    headImageView.userInteractionEnabled = YES;
    headImageView.layer.cornerRadius = 20 * SCREENH6;
    headImageView.layer.masksToBounds = YES;
    
    self.headImageView = headImageView;
    
    [self.backView addSubview:self.headImageView];
    // 名字视图
    UILabel * nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.headImageView.right + 10 * SCREENH6, self.headImageView.top, kTextWidth, 15 * SCREENH6)];
    nameLabel.font = [UIFont systemFontOfSize:13.0 * SCREENH6];
    nameLabel.textColor = [UIColor lightGrayColor];
    
    self.nameLab = nameLabel;
    
    [self.backView addSubview:self.nameLab];
    
    // 性别视图
    UIImageView * genderImageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.nameLab.left, self.nameLab.bottom + 3 * SCREENH6, 13 * SCREENH6, 13 * SCREENH6)];
    genderImageView.contentMode = UIViewContentModeScaleAspectFill;
    //    headImageView.userInteractionEnabled = YES;
    
    self.genderImageView = genderImageView;
    
    [self.backView addSubview:self.genderImageView];
    
    UILabel * timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.nameLab.left, self.genderImageView.bottom + 3 * SCREENH6, 150 * SCREENH6, 13 * SCREENH6)];
    
    timeLabel.textColor = ColorWithRGB(0x909090);
    timeLabel.font = [UIFont systemFontOfSize:11 * SCREENH6];
    
    self.timeLabel = timeLabel;
    
    [self.backView addSubview:self.timeLabel];
    
    
    UILabel * contentLabel = [[UILabel alloc] init];
    
    contentLabel.textColor = ZRBlackColor;
    contentLabel.font = [UIFont systemFontOfSize:13 * SCREENH6];
    contentLabel.numberOfLines = 0;
    contentLabel.userInteractionEnabled = YES;
    UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
    [contentLabel addGestureRecognizer:tapGR];
    
    self.contentLabel = contentLabel;
    
    [self.backView addSubview:self.contentLabel];
    
    UITextView *textView = [[UITextView alloc]init];
    textView.font = [UIFont systemFontOfSize:13 * SCREENH6];
    textView.backgroundColor = ColorWithRGB(0xF4F5F6);
    textView.delegate = self;
    textView.editable = NO;        //必须禁止输入，否则点击将弹出输入键盘
    textView.scrollEnabled = NO;
    textView.linkTextAttributes = @{NSForegroundColorAttributeName:ColorWithRGB(0xF9CF4A)};
    
    self.textView = textView;
    
    [self.backView addSubview:self.textView];
    
    UIView * lineView = [UIView new];

    lineView.backgroundColor = ColorWithRGB(0xe4e4e4);

    self.lineView = lineView;
    
    [self.backView addSubview:self.lineView];
    
}


- (void)tapAction{
    
    
}

- (BOOL)textView:(UITextView*)textView shouldInteractWithURL:(NSURL*)URL inRange:(NSRange)characterRange {
    
    NSRange range = [self.contentLabel.text rangeOfString:self.reply_user_name];
    
    if (NSEqualRanges(characterRange, range)) {
        
        if (self.delegate && [self.delegate respondsToSelector:@selector(clickNameWithUsr_id:)])
        {
            [self.delegate clickNameWithUsr_id:self.reply_user_id];
        }
    }

    return YES;
}



- (CGSize)preferredSizeWithMaxWidth:(CGFloat)maxWidth withLabel:(UILabel *)label
{
    
    CGSize size = [label sizeThatFits:CGSizeMake(maxWidth, 10000000.0f)];
    size.width = fmin(size.width, maxWidth); //在numberOfLine为1模式下返回的可能会比maxWidth大，所以这里我们限制下
    return size;
}


- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
